package understand;

import java.util.HashMap;
import java.util.Map;

public class LengthOfLongestSubstring {

    public static int lengthOfLongestSubstring(String s) {
        int ans = 0;
        int n = s.length();
        Map<Character, Integer> map = new HashMap<>();
        for (int start = 0, end = 0; end < n; end++) {
            Character alpha = s.charAt(end);
            if (map.containsKey(alpha)) {
                start = Math.max(start, map.get(alpha));
            }
            ans = Math.max(ans, end - start + 1);
            map.put(alpha, end + 1);
        }
        return ans;
    }

    public static void main(String[] args) {
        String s = "asddsdvasdassdvsasdasdasd";
        System.out.println(lengthOfLongestSubstring(s));
    }

}
